<?php
if (!defined('INWEB')) header("Location: ../index.php");
$a=getVar('a');
$id=getVar('id');
includeLang('octa');
switch($a)
{
    case 'add':
    if($_POST)
    {
        try
        {
            $code=getVar('code');
            $codename=getVar('code_name');
            $percent=getVar('percent','int');
            $ic=getVar('ins_comp','int',true);
            $ic=SQLNull($ic);

            $sql->query("SELECT * FROM `octa_discounts` WHERE `code`='$code';");
            if($sql->row_count)
            {
                msg($Lang['error'],$Lang['octa_discount_exists'], 'error');
            }
            else
            {
                $sql->query("INSERT INTO `octa_discounts` (`code`, `name`, `percent`, `ins_comp`) VALUES ('$code', '$codename', '$percent', $ic);");
                msg($Lang['success'],$Lang['octa_discount_added'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
            }
        }
        catch(Exception $e)
        {
            msg($Lang['error'],$e->getMessage(),'error');
        }
    }
    else
    {
        echo '<form action="?p='.$page.'&a=add" method="post"><table border="1">';
        echo '<tr><th>'.$Lang['code'].'</th><td><input type="text" maxlength="3" name="code" /></td></tr>';
        echo '<tr><th>'.$Lang['code_name'].'</th><td><input type="text" name="code_name" /></td></tr>';
        echo '<tr><th>'.$Lang['percent'].'</th><td><input maxlength="3" type="text" name="percent" value="0" /></td></tr>';
        echo '<tr><th>'.$Lang['ins_comp'].'</th><td><select name="ins_comp"><option value=""></option>';
        $ics=$sql->query("SELECT `id`, `abbr` FROM `insurance_companies`");
        while($ic=$sql->fetch_array($ics))
        {
            echo '<option value="'.$ic['id'].'">'.$ic['abbr'].'</option>';
        }
        echo '</select></td></tr>';
        echo'</table><input type="submit" class="yt_button" value="'.$Lang['add'].'" /></form>';
    }
    break;
    case 'edit':
        if($_POST)
        {
            try
            {
                $code=getVar('code');
                $codename=getVar('code_name');
                $percent=getVar('percent','int');
                $ic=getVar('ins_comp','int',true);
                $ic=SQLNull($ic);

                $sql->query("SELECT * FROM `octa_discounts` WHERE `id`='$id';");
                $check=$sql->fetch_array();
                if($check['code']!=$code)
                {
                    $sql->query("SELECT * FROM `octa_discounts` WHERE `code`='$code';");
                    if($sql->row_count)
                    {
                        msg($Lang['error'],$Lang['octa_discount_exists'], 'error');
                    }
                    else
                    {
                        $sql->query("UPDATE `octa_discounts` SET `code`='$code', `name`='$codename', `percent`='$percent', `ins_comp`=$ic WHERE id='$id';");
                        msg($Lang['success'],$Lang['octa_discount_updated'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
                    }
                }
                else
                {
                    $sql->query("UPDATE `octa_codes` SET `name`='$codename', `percent`='$percent', `ins_comp`=$ic WHERE id='$id';");
                    msg($Lang['success'],$Lang['octa_discount_updated'].'</br> <a href="?p='.$page.'">'.$Lang['back'].'</a>');
                }

            }
            catch(Exception $e)
            {
                msg($Lang['error'],$e->getMessage(),'error');
            }
        }
        else
        {
            $sql->query("SELECT * FROM `octa_discounts` WHERE id='$id';");
            $od=$sql->fetch_array();
            echo '<form action="?p='.$page.'&a=edit&id='.$id.'" method="post"><table border="1">';
            echo '<tr><th>'.$Lang['code'].'</th><td><input type="text" maxlength="3" name="code" value="'.$od['code'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['code_name'].'</th><td><input type="text" name="code_name" value="'.$od['name'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['percent'].'</th><td><input maxlength="3" type="text" name="percent" value="'.$od['percent'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['ins_comp'].'</th><td><select name="ins_comp"><option value=""></option>';
            $ics=$sql->query("SELECT `id`, `abbr` FROM `insurance_companies`");
            while($ic=$sql->fetch_array($ics))
            {
                $selected=($ic['id']==$od['ins_comp'])?' selected="selected"':'';
                echo '<option value="'.$ic['id'].'"'.$selected.'>'.$ic['abbr'].'</option>';
            }
            echo '</select></td></tr>';
            echo'</table><input type="submit" class="yt_button" value="'.$Lang['edit'].'" /></form>';
        }
    break;
    case 'delete':
        if(isset($_GET['confirm']))
        {
            $sql->query("DELETE FROM `octa_discounts` WHERE id='$id';");
            msg($Lang['success'],$Lang['octa_discount_deleted']);
        }
        else
        {
            msg($Lang['warning'],$Lang['are_u_sure_delete_octa_discount'].$id.' ?<br /> <a href="?p='.$page.'&a=delete&id='.$id.'&confirm">'.$Lang['yes'].'</a>/<a href="?p='.$page.'">'.$Lang['no'].'</a>','warning');
        }
    break;
    default:
        echo '<a href="?p='.$page.'&a=add"><img src="img/add.png" alt="'.$Lang['add'].'" title="'.$Lang['add'].'" /></a><table border="1">';
        echo '<tr><td>'.$Lang['code'].'</td><td>'.$Lang['code_name'].'</td><td>'.$Lang['percent'].'</td><td>'.$Lang['ins_comp'].'</td><td>'.$Lang['actions'].'</td></tr>';

        $ods=$sql->query("SELECT * FROM `octa_discounts`;");
        while($od=$sql->fetch_array($ods))
        {
            if($od['ins_comp']!='')
            {
                $icq=$sql->query("SELECT `abbr` FROM `insurance_companies` WHERE `id`='{$od['ins_comp']}'");
                $icf=$sql->fetch_array($icq);
                $ic=$icf['abbr'];
            }
            $options=($od['ins_comp']=='')?'<option></option>':'<option>'.$ic.'</option>';
            
            echo '<tr><td>'.$od['code'].'</td><td>'.$od['name'].'</td><td>'.$od['percent'].'</td><td><select>'.$options.'</select></td><td><a href="?p='.$page.'&a=edit&id='.$od['id'].'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=delete&id='.$od['id'].'"><img src="img/delete.png" alt="'.$Lang['delete'].'" title="'.$Lang['delete'].'" /></a></td></tr>';
        }
        echo '</table>';
    break;
}
?>